package com.ruoyi.huoxingshi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.huoxingshi.utils.QCloudCosUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.huoxingshi.domain.HxsEpisodes;
import com.ruoyi.huoxingshi.service.IHxsEpisodesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 短剧选集Controller
 *
 * @author huoxingshi
 * @date 2025-06-27
 */
@RestController
@RequestMapping("/huoxingshi/episodes")
public class HxsEpisodesController extends BaseController {
    @Autowired
    private IHxsEpisodesService hxsEpisodesService;
    @Autowired
    private QCloudCosUtil qCloudCosUtil;

    /**
     * 查询短剧选集列表
     */
    @PreAuthorize("@ss.hasPermi('huoxingshi:episodes:list')")
    @GetMapping("/list")
    public TableDataInfo list(HxsEpisodes hxsEpisodes) {
        startPage();
        List<HxsEpisodes> list = hxsEpisodesService.selectHxsEpisodesList(hxsEpisodes);
        return getDataTable(list);
    }

    /**
     * 导出短剧选集列表
     */
    @PreAuthorize("@ss.hasPermi('huoxingshi:episodes:export')")
    @Log(title = "短剧选集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HxsEpisodes hxsEpisodes) {
        List<HxsEpisodes> list = hxsEpisodesService.selectHxsEpisodesList(hxsEpisodes);
        ExcelUtil<HxsEpisodes> util = new ExcelUtil<HxsEpisodes>(HxsEpisodes.class);
        util.exportExcel(response, list, "短剧选集数据");
    }

    /**
     * 获取短剧选集详细信息
     */
    @PreAuthorize("@ss.hasPermi('huoxingshi:episodes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hxsEpisodesService.selectHxsEpisodesById(id));
    }

    /**
     * 新增短剧选集
     */
    @PreAuthorize("@ss.hasPermi('huoxingshi:episodes:add')")
    @Log(title = "短剧选集", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HxsEpisodes hxsEpisodes) {
        return toAjax(hxsEpisodesService.insertHxsEpisodes(hxsEpisodes));
    }

    /**
     * 修改短剧选集
     */
    @PreAuthorize("@ss.hasPermi('huoxingshi:episodes:edit')")
    @Log(title = "短剧选集", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HxsEpisodes hxsEpisodes) {
        return toAjax(hxsEpisodesService.updateHxsEpisodes(hxsEpisodes));
    }

    /**
     * 删除短剧选集
     */
    @PreAuthorize("@ss.hasPermi('huoxingshi:episodes:remove')")
    @Log(title = "短剧选集", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hxsEpisodesService.deleteHxsEpisodesByIds(ids));
    }


    /**
     * 短剧视频上传
     */
    @PostMapping("/upload")
    public AjaxResult uploadVideo(MultipartFile file) throws Exception {
        try {
//            QCloudCosUtil.UploadResult uploadResult = qCloudCosUtil.uploadFile(new File("C:\\Users\\Administrator\\Pictures\\火星石\\bt2.mp4"));
            QCloudCosUtil.UploadInfo uploadInfo = qCloudCosUtil.uploadFile(file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", uploadInfo.getUrl());
            ajax.put("fileName", uploadInfo.getFileName());
            ajax.put("newFileName", uploadInfo.getNewFileName());
            ajax.put("originalFilename", uploadInfo.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
